﻿//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
//
//*********************************************************

#pragma once
#include "pch.h"
#include "DirectXPanelBase.h"
#include "StepTimer.h"
#include "ShaderStructures.h"
#include <map>

namespace SharpDepend_UWP_DirectXInterop
{
    // Hosts a DirectX rendering surface that draws a simple tic-tac-toe board using Direct2D.  This panel optionally
    // updates the size of its swap chain to match the current composition scale if the IsSwapChainSizeScaled property is set
    // to true.

    [Windows::Foundation::Metadata::WebHostHidden]
    public ref class D2DPanel sealed : public SharpDepend_UWP_DirectXInterop::DirectXPanelBase
    {
    public:
        D2DPanel();

	private protected:
		void OnBeginDraw() override;
		void OnDrawRectangle(
			float x1, float y1, float x2, float y2,
			float r, float g, float b, float a,
			int textureId,
			float td_x1, float td_y1, float td_x2, float td_y2, 
			int textureWidth, int textureHeight) override;
		void OnDrawLine(
			float x1, float y1, float x2, float y2,
			float r, float g, float b, float a) override;
		void OnDrawCircle(
			float x, float y, float radius,
			float r, float g, float b, float a) override;
		void OnDrawTriangles(Vector2Double* vertices, int verticesLength, unsigned short* indices, int indicesLength, ColorUInt* colors, int colorLength) override;
		void OnEndDraw() override;

		void OnRegisterTexture(int id, int* data, int dataLength, int width, int height) override;
		void OnUnregisterTexture(int id) override;

	private:
		union ColorUnion
		{
			struct iColor
			{
				unsigned char r;
				unsigned char g;
				unsigned char b;
				unsigned char a;
			};

			iColor Color;
			int Packed;
		};

		std::map<int, ID2D1Bitmap*> m_textures;

    private protected:
        virtual void Render() override;
        virtual void CreateDeviceResources() override;

        virtual void OnDeviceLost() override;
        virtual void OnSizeChanged(Platform::Object^ sender, Windows::UI::Xaml::SizeChangedEventArgs^ e) override;
        virtual void OnCompositionScaleChanged(Windows::UI::Xaml::Controls::SwapChainPanel ^sender, Platform::Object ^args) override;
        virtual void OnResuming(Platform::Object^ sender, Platform::Object^ args) override;

        Microsoft::WRL::ComPtr<ID2D1SolidColorBrush>                        m_strokeBrush;
    };
}